package com.neusoft.elm.view.impl;


import java.util.List;
import java.util.Scanner;

import com.neusoft.elm.dao.BusinessDao;
import com.neusoft.elm.dao.FoodDao;
import com.neusoft.elm.dao.impl.BusinessDaoImpl;
import com.neusoft.elm.dao.impl.FoodDaoImpl;
import com.neusoft.elm.po.Food;
import com.neusoft.elm.view.FoodView;

public class FoodViewImpl implements FoodView {
	
	private Scanner input = new Scanner(System.in);
	
	@Override
	public List<Food> showFoodList(Integer businessId) {
		FoodDao dao = new FoodDaoImpl();
		List<Food> list = dao.listFoodByBusinessId(businessId);
		System.out.println("食品编号\t食品名称\t食品介绍\t食品价格\t商家编号");
		for(Food f:list){
			System.out.println(f.getFoodId()+"\t"+
								f.getFoodName()+"\t"+
								f.getFoodExplain()+"\t"+
								f.getFoodPrice()+"\t"+
								f.getFoodPrice()+"\t"+
								f.getBusinessId());			
		}
		
		return list;
	}

	@Override
	public void saveFood(Integer businessId) {
		Food food = new Food();
		System.out.println("请输入食品的名称：");
		food.setFoodName(input.next());
		System.out.println("请输入食品介绍：");
		food.setFoodExplain(input.next());
		System.out.println("请输入食品价格：");
		food.setFoodPrice(input.nextDouble());
		food.setBusinessId(businessId);
		FoodDao dao = new FoodDaoImpl();
		int result = dao.saveFood(food);
		if(result>0){
			System.out.println("新增食品成功");
		}else{
			System.out.println("新增食品失败");
		}
		
	}

	@Override
	public void updateFood(Integer foodId) {
		FoodDao dao = new FoodDaoImpl();
		
		List<Food> foodList = showFoodList(foodId);
		if(foodList.size()==0){
			System.out.println("没有食品信息，请新建");
		}else{
		System.out.println("输入你要修改的食品编号");
		/*System.out.println(food.toString());*/
		String inputStr = "";
		int setFoodId=input.nextInt();
		Food food = dao.getFoodById(setFoodId);
		System.out.println(food);
		
		System.out.println("===================");
		System.out.println("是否修改食品名称(y/n)");
		inputStr=input.next();
		if(inputStr.equals("y")){
			System.out.println("请输入新的食品名称");
			 food.setFoodName(input.next());
		}
		
		System.out.println("是否修改食品介绍(y/n)");
		inputStr=input.next();
		if(inputStr.equals("y")){
			System.out.println("请输入新的食品介绍");
			food.setFoodExplain(input.next());
		}
		
		System.out.println("是否修改食品价格(y/n)");
		inputStr=input.next();
		if(inputStr.equals("y")){
			System.out.println("请输入新的食品价格");
			food.setFoodPrice(input.nextDouble());
		}
		
		int result = dao.updateFood(food);
		if(result>0){
			System.out.println("修改食品信息成功");
		}else{
			System.out.println("修改食品信息失败");
		}
	}
	
	}

	@Override
	public void removeFood(Integer businessId) {
		FoodDao dao = new FoodDaoImpl();
		int result=0;
		List<Food> foodList = showFoodList(businessId);
		if(foodList.size()==0){
			System.out.println("没有食品信息");
		}else{
		System.out.println("请输入要删除的食品编号");
		/*System.out.println(food.toString());*/
		
		int delFoodId=input.nextInt();
		Food food = dao.getFoodById(delFoodId);
		System.out.println(food);
		System.out.println("===================");
		System.out.println("你确认要删除吗(y/n):");
		if(input.next().equals("y")){
			result = dao.removeFood(delFoodId);
			if(result>0){
				System.out.println("删除成功");
			}else{
				System.out.println("删除失败");
			}
		}
		
		}

	}
}